﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebBanThuoc.Function;
using WebBanThuoc.Server;
using WebBanThuoc.DAO;
using WebBanThuoc.DTO;

namespace WebBanThuoc.admin
{
    public partial class QLSanPham : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!FuncAdmin.IsLogin(Session))
            {
                Session.Add("URLCurrentActive_SendTo_Login", Request.RawUrl);
                Response.Redirect("/admin/DangNhap.aspx");
            }

            
            
            if (!IsPostBack)
            {
                
            }
            else
            {
                if (checkDefaultSubmit())
                {
                    string MaSanPham = Request.Params["MaSanPham"];
                    if (MaSanPham != null && MaSanPham.Trim() != "")
                    {
                        IServerWeb sw = new ServerWeb();
                        SanPham_DAO daoSP = new SanPham_DAO(sw.Connection);
                        SanPham sp = daoSP.getByKey("SP" + MaSanPham);
                        sw.closeConnection();
                        if (sp != null)
                        {
                            Session.Add("page_QLSanPham_dataSP", sp);

                            CachDung.Text = sp.CachDung;
                            ThanhPhan.Text = sp.ThanhPhan;
                            CongDung.Text = sp.CongDung;
                        }
                        else
                        {
                            CachDung.Text = "";
                            ThanhPhan.Text = "";
                            CongDung.Text = "";
                        }
                    }
                }
                else
                {
                    if (Request.Params["btn_Them"] != null)
                    {
                        addProduct();
                    }
                    if (Request.Params["btn_Sua"] != null)
                    {
                        updateProduct();
                    }
                    if (Request.Params["btn_Xoa"] != null)
                    {
                        deleteProduct();
                    }
                }
            }

            this.LoadComplete += new EventHandler(QLSanPham_LoadComplete);
        }

        void QLSanPham_LoadComplete(object sender, EventArgs e)
        {
            getDataGeneral();
        }

        private bool checkDefaultSubmit()
        {
            if (Request.Params["btn_Xoa"] != null || Request.Params["btn_Sua"]!= null || Request.Params["btn_Them"] !=null)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// Lấy data mặc định cho page
        /// </summary>
        private void getDataGeneral()
        {
            IServerWeb sw = new ServerWeb();
            NhaSanXuat_DAO daoNSX = new NhaSanXuat_DAO(sw.Connection);
            LoaiSanPham_DAO daoLSP = new LoaiSanPham_DAO(sw.Connection);
            SanPham_DAO daoSP = new SanPham_DAO(sw.Connection);
            List<NhaSanXuat> nsx = daoNSX.getListByKey();
            List<LoaiSanPham> lsp = daoLSP.getListByKey();
            string msp = daoSP.createMaSanPham();
            Session.Add("page_QLSanPham_dataListNSX", nsx);
            Session.Add("page_QLSanPham_dataListLSP", lsp);
            Session.Add("page_QLSanPham_MaSanPham_Moi", msp);
            sw.closeConnection();
        }


        private void addProduct()
        {
            string MSP = Request.Params["MaSanPham"];
            if (MSP != null || MSP.ToString().Trim() == "")
            {
                SanPham sp = new SanPham();
                IServerWeb sw = new ServerWeb();
                SanPham_DAO daoSP = new SanPham_DAO(sw.Connection);
                sp.TenSanPham = Request.Params["TenSanPham"].ToString();
                sp.ThanhPhan = ThanhPhan.Text;
                sp.CongDung = CongDung.Text;
                sp.CachDung = CachDung.Text;
                sp.MaLoaiSanPham = Request.Params["MaLoaiSanPham"].ToString();
                sp.MaNhaSanXuat = Request.Params["MaNhaSanXuat"].ToString();
                sp.GiaBan = Function.Func.convertDoubleToString(Request.Params["GiaBan"].ToString());
                sp.KhuyenMai = Request.Params["KhuyenMai"].ToString();                
                sp.GiaDaGiam = Function.Func.convertDoubleToString(Request.Params["GiaDaGiam"].ToString());
                if (sp.GiaDaGiam == 0)
                {
                    sp.GiaDaGiam = sp.GiaBan;
                }
                sp.IsNew = Request.Params["IsNew"] == null ? false : true;
                sp.CoHang = Request.Params["CoHang"] == null ? false : true;
                sp.MoTa = Request.Params["MoTa"].ToString();
                //sp.MaSanPham = daoSP.createMaSanPham();
                int tempRS = daoSP.add(sp);
                //tempRS = 1;
                if (tempRS > 0)
                {
                    SanPham spNew = daoSP.getByKey(sp.MaSanPham);
                    Session.Add("page_QLSanPham_msgSucessFunc", "Đã thêm thành công sản phẩm mới " + spNew.MaSanPham);
                    CachDung.Text = "";
                    ThanhPhan.Text = "";
                    CongDung.Text = "";

                    KhachHang_DAO daoKH = new KhachHang_DAO(sw.Connection);
                    HinhAnh_DAO daoHA = new HinhAnh_DAO(sw.Connection);
                    List<string> mailBcc = (from d in daoKH.getListByKey() select d.Email).ToList();
                    List<HinhAnh> hinhAnh = daoHA.getListByKey(spNew.MaSanPham);

                    string path = System.Configuration.ConfigurationManager.AppSettings["AnhSanPham"].Trim().Replace("..", "");
                    Function.Func ff = new Function.Func();
                    string sub = spNew.TenSanPham + "<br />";
                    if (hinhAnh.Count > 0)
                    {
                        sub += " <u> Ảnh sản phẩm </u> : <br />" +
                            "<p><img alt='' src='" + hinhAnh[0].DuongDan.Replace("..", Request.Url.Authority)
                            + "' style='max-width:200px; max-height:200px' /></p>";
                    }
                    if (sp.ThanhPhan != "")
                    {
                        sub += "<br /> <u> Thành phần </u> : <br />"
                            + sp.ThanhPhan;
                    }
                    if (sp.CongDung != "")
                    {
                        sub += "<br /> <u> Công dụng </u> : <br />"
                            + sp.CongDung;
                    }
                    //if (sp.CachDung != "")
                    //{
                    //    sub += "<br /> <u> Cách dùng </u> : + <br />"
                    //        + sp.CachDung;
                    //}
                    sub += "<br /> <u> Hãy bấm vào link dưới để xem chi tiết sản phẩm mới này </u> : <br />"
                        + "http://www.happyfamilyshop.vn/CTSanPham.aspx?maSanPham=" + sp.MaSanPham;

                    ff.sendMailNew_Many("", new List<string>(), mailBcc, "Happyfamifyshop.vn - Sản phẩm mới - " + spNew.TenSanPham,
                        sub);

                }
                sw.closeConnection();
            }
        }

        private void updateProduct()
        {
            string MSP = Request.Params["MaSanPham"];
            if (MSP != null || MSP.ToString().Trim() != "")
            {
                
                IServerWeb sw = new ServerWeb();
                SanPham_DAO daoSP = new SanPham_DAO(sw.Connection);
                //SanPham sp = daoSP.getByKey("SP" + Request.Params["MaSanPham"].ToString().Trim());
                SanPham sp = daoSP.getByKey("SP" + MSP.Trim());

                sp.TenSanPham = Request.Params["TenSanPham"].ToString();
                sp.ThanhPhan = ThanhPhan.Text;
                sp.CongDung = CongDung.Text;
                sp.CachDung = CachDung.Text;
                sp.MaLoaiSanPham = Request.Params["MaLoaiSanPham"].ToString();
                sp.MaNhaSanXuat = Request.Params["MaNhaSanXuat"].ToString();
                sp.GiaBan = Function.Func.convertDoubleToString(Request.Params["GiaBan"].ToString());
                sp.KhuyenMai = Request.Params["KhuyenMai"].ToString();
                sp.GiaDaGiam = Function.Func.convertDoubleToString(Request.Params["GiaDaGiam"].ToString());
                if (sp.GiaDaGiam == 0)
                {
                    sp.GiaDaGiam = sp.GiaBan;
                }
                sp.IsNew = Request.Params["IsNew"] == null ? false : true;
                sp.CoHang = Request.Params["CoHang"] == null ? false : true;
                sp.MoTa = Request.Params["MoTa"].ToString();
                //sp.MaSanPham = daoSP.createMaSanPham();
                int tempRS = daoSP.updateByKey(sp, sp.MaSanPham);
                if (tempRS > 0)
                {
                    SanPham spNew = daoSP.getByKey(sp.MaSanPham);
                    Session.Add("page_QLSanPham_msgSucessFunc", "Đã Cập nhật thành công sản phẩm " + spNew.MaSanPham);
                    Session.Add("page_QLSanPham_dataSP", spNew);
                    CachDung.Text = spNew.CachDung;
                    ThanhPhan.Text = spNew.ThanhPhan;
                    CongDung.Text = spNew.CongDung;
                }
                sw.closeConnection();
            }
        }

        private void deleteProduct()
        {
            string MSP = Request.Params["MaSanPham"];
            if (MSP != null || MSP.ToString().Trim() != "")
            {

                IServerWeb sw = new ServerWeb();
                SanPham_DAO daoSP = new SanPham_DAO(sw.Connection);
                
                //sp.MaSanPham = daoSP.createMaSanPham();
                int tempRS = daoSP.deleteByKey("SP" + MSP);
                if (tempRS > 0)
                {                    
                    Session.Add("page_QLSanPham_msgSucessFunc", "Đã Xóa thành công sản phẩm SP" + MSP);                    
                    CachDung.Text = "";
                    ThanhPhan.Text = "";
                    CongDung.Text = "";
                }
                sw.closeConnection();
            }
        }
        
    }
}